﻿using Auxiliary.VecMath;
using Simple3DRedactor.Domain;
using Tao.OpenGl;

namespace Simple3DRedactor.OpenGLViewer
{
    class BoxView : GeometryView<Box>
    {
        public BoxView(Box obj, ObjectViewFactory factory) : base(obj, factory)
        {
        }

        protected override void RenderImpl()
        {
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3f(0.0f, 1.0f, 0.0f);
            Gl.glVertex3f(0.0f, InnerObject.Height, 0.0f);
            Gl.glVertex3f(0.0f, InnerObject.Height, InnerObject.Depth);
            Gl.glVertex3f(InnerObject.Width, InnerObject.Height, InnerObject.Depth);
            Gl.glVertex3f(InnerObject.Width, InnerObject.Height, 0.0f);

            Gl.glNormal3f(0.0f, -1.0f, 0.0f);
            Gl.glVertex3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(InnerObject.Width, 0.0f, 0.0f);
            Gl.glVertex3f(InnerObject.Width, 0.0f, InnerObject.Depth);
            Gl.glVertex3f(0.0f, 0.0f, InnerObject.Depth);
            
            Gl.glNormal3f(0.0f, 0.0f, 1.0f);
            Gl.glVertex3f(0.0f, InnerObject.Height, InnerObject.Depth);
            Gl.glVertex3f(0.0f, 0.0f, InnerObject.Depth);
            Gl.glVertex3f(InnerObject.Width, 0.0f, InnerObject.Depth);
            Gl.glVertex3f(InnerObject.Width, InnerObject.Height, InnerObject.Depth);

            Gl.glNormal3f(0.0f, 0.0f, -1.0f);
            Gl.glVertex3f(0.0f, InnerObject.Height, 0.0f);
            Gl.glVertex3f(InnerObject.Width, InnerObject.Height, 0.0f);
            Gl.glVertex3f(InnerObject.Width, 0.0f, 0.0f);
            Gl.glVertex3f(0.0f, 0.0f, 0.0f);

            Gl.glNormal3f(1.0f, 0.0f, 0.0f);
            Gl.glVertex3f(InnerObject.Width, InnerObject.Height, InnerObject.Depth);
            Gl.glVertex3f(InnerObject.Width, 0.0f, InnerObject.Depth);
            Gl.glVertex3f(InnerObject.Width, 0.0f, 0.0f);
            Gl.glVertex3f(InnerObject.Width, InnerObject.Height, 0.0f);

            Gl.glNormal3f(-1.0f, 0.0f, 0.0f);
            Gl.glVertex3f(0.0f, InnerObject.Height, InnerObject.Depth);
            Gl.glVertex3f(0.0f, InnerObject.Height, 0.0f);
            Gl.glVertex3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(0.0f, 0.0f, InnerObject.Depth);
            Gl.glEnd();
        }

        private const float Bound = 0.1f;

        protected override Vector3D MaxPoint
        {
            get { return new Vector3D(InnerObject.Width + Bound, InnerObject.Height + Bound, InnerObject.Depth + Bound); }
        }

        protected override Vector3D MinPoint
        {
            get { return new Vector3D(-Bound, -Bound, -Bound); }
        }
    }
}
